package com.hmall.common.exception;

import com.hmall.common.dto.ErrorResult;
import com.hmall.common.dto.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(value = BusinessException.class)
    public Object stockExceptionHandler(BusinessException be) {
        be.printStackTrace();
        ErrorResult errorResult = be.getErrorResult();
        return Response.failure(errorResult.getErrMessage());
    }

    @ExceptionHandler(Exception.class)
    public Response handlerException(Exception ex) {
        ex.printStackTrace();
        return Response.failure(ErrorResult.error().getErrMessage());
    }
}